/**
 * Copyright 2009, by the authors.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package com.google.code.docbkx.incubator.xmlconverter;

import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.codehaus.plexus.util.DirectoryScanner;

import java.io.File;
import java.io.IOException;

/**
 *
 */
public abstract class AbstractXMLConverterMojo extends AbstractMojo implements Converter
{

  public AbstractXMLConverterMojo()
  {
    super();
  }

  /**
   * The base directory, in which to search for files.
   *
   * @parameter expression="${xmlconverter.basedir}" default-value="${basedir}"
   * @required
   */
  protected File sourceDirectory;

  /**
   * Specifies files, which are included. By default, all files are included.
   *
   * @parameter
   */
  protected String[] includes = new String[0];

  /**
   * Specifies files, which are excluded. By default, no files are excluded.
   *
   * @parameter
   */
  protected String[] excludes = new String[0];

  /**
   * Set this to "true" to cause no output
   *
   * @parameter expression="${xmlconverter.quiet}" default-value="false"
   */
  protected boolean quiet = false;

  /**
   * @parameter expression="${buildir}
   */
  private String targetDiretory;

  public void execute() throws MojoExecutionException, MojoFailureException
  {
    final DirectoryScanner scanner = new DirectoryScanner();
    scanner.setBasedir(sourceDirectory);
    scanner.setIncludes(includes);
    scanner.setExcludes(excludes);
    scanner.scan();

    final String[] included = scanner.getIncludedFiles();
    for (final String include : included)
    {
      String filename = include;
      int extension = include.lastIndexOf(".");
      if (extension < 0)
      {
        filename += ".xml";
      }
      else
      {
        filename = filename.substring(0, extension) + ".xml";
      }

      final File targetFile = new File(targetDiretory, filename);
      final File sourceFile = new File(sourceDirectory, include);

      try
      {
        convert(sourceFile, targetFile);
      }
      catch (IOException e)
      {
        getLog().error("Unable to convert one file in XML", e);
      }
    }
  }

  protected final void info(String format, Object... params)
  {
    if (!quiet)
    {
      getLog().info(String.format(format, params));
    }
  }

  protected final void debug(String format, Object... params)
  {
    if (!quiet)
    {
      getLog().debug(String.format(format, params));
    }
  }

  protected final void warn(String format, Object... params)
  {
    getLog().warn(String.format(format, params));
  }

  protected final void error(String format, Object... params)
  {
    getLog().error(String.format(format, params));
  }
}
